/*
 *  Jiki - a small lightweight wiki engine.
 *
 *  Copyright 2008 Peter Halbmayer <phalbmayer@gmail.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

package at.pmsa.jiki.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IOMan implements DataMan{
	static File root;
	public void writeFile(String fileName, String contents){
//		root = new File(System.getProperty("user.home") + File.separator + "jiki");
//		root = new File(System.getProperty("user.dir") + File.separator + "jiki");
		root = new File("/jiki");
		
		try{
//			FileWriter fstream = new FileWriter(root + File.separator + fileName);
//			FileOutputStream fstream = new FileOutputStream(root + File.separator + fileName);
//			BufferedWriter out = new BufferedWriter(fstream);

//			FileOutputStream fstream = ApplicationContext.openFileOutput("myFileName.png",Activity.MODE_PRIVATE); 
			BufferedOutputStream fstream = new BufferedOutputStream(new FileOutputStream(root + 
					File.separator + fileName));
			
			fstream.write(contents.getBytes(), 0, contents.length());
			fstream.close();
		}
		catch(IOException ioe){
			ioe.printStackTrace();
			System.err.println("Problem writing to file " + fileName);
//			System.exit(0);
		}
	}

	public String readFile(String fileName){
		StringBuffer buffer = new StringBuffer();
//		root = new File(System.getProperty("user.home") + File.separator + "jiki");
//		root = new File(System.getProperty("user.dir") + File.separator + "jiki");
		root = new File("/jiki");
		
		try{
//		   BufferedReader reader = new BufferedReader(new FileReader(root + File.separator + fileName));
//		   FileInputStream reader = new FileInputStream(root + File.separator + fileName);
		   BufferedInputStream reader = new BufferedInputStream(new FileInputStream(root + 
				   File.separator + fileName));
		   
//		   buffer = new StringBuffer();
		   // String that holds current file line
//		   String line;
		   char ch;
		   
		   while((ch = (char)reader.read()) != -1){
			   buffer.append(ch);
			   System.err.println("Problem reading from file " + fileName);
//			   buffer.append('\n');
		   }
		   reader.close();
		}
		catch(ArrayIndexOutOfBoundsException e){
			e.printStackTrace();
			System.err.println("Problem reading from file " + fileName);
			return null;
		}
		catch(IOException e){
		   e.printStackTrace();
		   System.err.println("Problem reading from file " + fileName);
		   return null;
		}
		return buffer.toString();
	}
	
	public String[] getAllFileNames(){
		return root.list();
	}
	
	public boolean fileExists(String name){
		return false;
	}
}
